package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;



public class DialogAgreementHint {

    private Context cxt;


    public DialogAgreementHint(Context cxt) {
        this.cxt = cxt;
    }

    public void show(View.OnClickListener onUserAgreementClick, View.OnClickListener onPrivacyStatementClick, View.OnClickListener onYes, View.OnClickListener onNo) {
        AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
        AlertDialog alertDialog = builder.create();
        alertDialog.setCancelable(false);

        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_agreement_hint, null);
        TextView tvContent = view.findViewById(R.id.tv_dialog_agreement_hint_content);
        SpannableString spannableString = new SpannableString(cxt.getResources().getString(R.string.agreement_hint));
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                onUserAgreementClick.onClick(view);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
            }
        }, 14, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                onPrivacyStatementClick.onClick(view);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
            }
        }, 21, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(spannableString);
        tvContent.setMovementMethod(new LinkMovementMethod());
        Button btYes = view.findViewById(R.id.bt_dialog_agreement_hint_yes);
        Button btNo = view.findViewById(R.id.bt_dialog_agreement_hint_no);

        btYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
                onYes.onClick(view);
            }
        });

        btNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
                onNo.onClick(view);
            }
        });

        alertDialog.setView(view);
        alertDialog.show();

    }


}
